package javautils;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

public class ByteUtils {

	// 把byte[]转成整型
	public static int byte2Int(byte[] b) {
		return (b[0] << 24) 
		+ ((b[1] & 0xFF) << 16) 
		+ ((b[2] & 0xFF) << 8)
		+ (b[3] & 0xFF);
	}

	// 把整型转成byte[]
	public static byte[] int2Byte(int value) {
		return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16),
				(byte) (value >>> 8), (byte) value };
	}

	// 把bytes转成Bitmap 未测试
	private Bitmap Bytes2Bimap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		} else {
			return null;
		}
	}

	// 把Bitmap转成byte[] 未测试
	private byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}
    //把drawable转成bitmap 未测试
	public static Bitmap drawableToBitmap(Drawable drawable) {

		Bitmap bitmap = Bitmap
				.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		// canvas.setBitmap(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}
    //两个byte[]合并 a+b
	public byte[] arrayCopy(byte[] a, byte[] b) {

		byte[] c = null;
		int alength = a.length;
		int blength = b.length;
		int clength = a.length + b.length;
		c = new byte[clength];
		System.arraycopy(a, 0, c, 0, alength);
		System.arraycopy(b, 0, c, alength, blength);
		for (int i = 0; i < clength; i++) {
			System.out.println(c[i]);
		}
		return c;
	}
}
